package com.wang.strategy;


import com.wang.client.RpcClient;
import org.springframework.util.CollectionUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * zk多机负载均衡策略
 */
public class MultiLoadBalanceStrategy implements LoadBalanceStrategy {

    @Override
    public RpcClient getClient(Map<String, RpcClient> rpcClientMap) {
        if (CollectionUtils.isEmpty(rpcClientMap)) {
            return null;
        }
        //按响应时间排序取耗时最短的
        List<RpcClient> collect = rpcClientMap.values().stream().sorted(Comparator.comparingLong(RpcClient::getCost)).collect(Collectors.toList());
        return collect.get(0);
    }
}
